package com.john.booter.module.role;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.john.booter.annotation.Module;
import com.john.booter.annotation.OptionName;
import com.john.booter.annotation.SubModuleName;
import com.john.booter.domain.entity.Role;
import com.john.booter.exception.RestSuccess;
import com.john.booter.exception.RestSuccess.Msg;
import com.john.booter.module.role.service.RoleService;
import com.john.booter.pagination.DataTable;
import com.john.booter.pagination.Pager;

@SubModuleName(value = "角色管理", root = Module.user, sort=1)
@RestController
@RequestMapping("/role")
public class RoleCtrl {
	
	@Autowired
	private RoleService service;
	
	@OptionName("列表-WEB")
	@RequestMapping("/datatable")
	public DataTable<Role> list(
			@RequestParam(value = "start") Integer start,
			@RequestParam(value = "length") Integer pageSize,
			@RequestParam(value = "search[value]") String searchValue,
			@RequestParam(value = "order[0][column]") Integer orderColumn,
			@RequestParam(value = "order[0][dir]") String orderDir,
			@RequestParam(value = "draw") Integer draw,
			@RequestParam(value = "username", required = false) String username) {
		start = ((start == null || start <= 0) ? 0 : start);
		pageSize = (pageSize == null ? Pager.DEFAULT_PAGE_SIZE : pageSize);
		draw = ((draw == null || draw == 0) ? 1 : draw);
		Pager pg = new Pager();
		pg.setStart(start);
		pg.setPageSize(pageSize);
		pg.setSearchValue(searchValue);
		pg.setOrderColumn(orderColumn);
		pg.setOrderDir(orderDir);
		pg.setDraw(draw);
		DataTable<Role> data = service.findDataTable(pg, username);
		return data;
	}
	
	@OptionName("新增")
	@RequestMapping("/add")
	public RestSuccess add(@RequestBody Role r) {
		service.add(r);
		return new RestSuccess(Msg.add);
	}
	
	@OptionName("删除")
	@RequestMapping("/delete/{code}")
	public RestSuccess delete(@PathVariable("code") String code) {
		service.delete(code);
		return new RestSuccess(Msg.delete);
	}
	
	@OptionName("批量删除")
	@RequestMapping("/batchDelete")
	public RestSuccess batchDelete(@RequestParam("codes[]") List<String> codes) {
		service.batchDelete(codes);
		return new RestSuccess(Msg.delete);
	}
	
	@OptionName("更新")
	@RequestMapping("/update")
	public RestSuccess update(@RequestBody Role r) {
		service.update(r);
		return new RestSuccess(Msg.update);
	}
	
	@OptionName("详情")
	@RequestMapping("/get/{code}")
	public Role get(@PathVariable("code") String code) {
		return service.findByCode(code);
	}
	
	@OptionName("更新角色菜单")
	@RequestMapping("/updateoption")
	public RestSuccess updateAuths(@RequestParam(value="roleCode") String roleCode,
			@RequestParam(value="optionCodes") String optionCodes) {
		service.updateRoleOptions(roleCode, optionCodes);
		return new RestSuccess(Msg.update);
	}
	
	@RequestMapping("/valid")
	public String valid(@RequestParam("code") String code) {
		if(service.findByCode(code) == null){
			return "{\"valid\":true}";
		}
		return "{\"valid\":false}";
	}
	
	@RequestMapping(value="/tree/{code}")
	public String tree(@PathVariable("code") String code) {
		return service.getRoleOptionsTree(code);
	}
	
	@RequestMapping(value="/list")
	public List<Role> getRoleList() {
		return service.findAll();
	}
	
	
}
